/**
* Copyright (c) 2010 Tony Zhou (http://www.cnblogs.com/zhouto/)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
* version: jquery.groupTable.js 12/14/2010 1:58pm
**/
;
(function (jQuery) {
    $.fn.extend({
        groupTable: function (options) {
            var settings = { groupby: "0", collapse: 0 };
            $.extend(settings, options);
            console.log(settings, options, 'settings, options');
            var tb = $(this);
            console.log(tb, 'tb');
            var id = tb.attr("id");
            var header = $("#" + id + " thead th"); // 定位要操作的表格的所有标题行
            var rows = $("#" + id + " tbody tr"); // 定位要操作的表格的子行 
            console.log(id, header, rows, 'id, header,rows');
            var groupby = settings.groupby;
            var groups = groupby.split(","); // 要操作的列
            var preValues = new Array(groups.length);
            var curValues = new Array(groups.length);
            console.log(preValues, curValues, 'preValues, curValues');
            var columnCount = header.length;
            var rowCount = rows.length;
            // 删除标题列
            $.each(groups, function (j, group) {
                header.eq(group).remove();
            });
            header = tb.find("thead th"); // 找到头部
            header.eq(0).before("<th colspan=" + groups.length + " class='left_space'></th>"); // 在头部第一个前面加入空格
            rows.each(function (i, row) {
                var precategory = "";
                var category = "";
                $.each(groups, function (j, group) {
                    curValues[j] = $(row).find("TD").eq(group).html();
                    category = curValues[j].replace(" ", "_");
                    if (precategory != "")
                        category = precategory + "_" + category;
                    if (curValues[j] != preValues[j]) {
                        if (precategory != "") {
                            $(row).before("<tr class='" + precategory + "'><td colspan=" + columnCount + "><div  class='blank' width=" + 9 * j + "px></div><div  class='minus' groupby='" + category + "' title='" + curValues[j] + "'></div><div class='blank'></div>" + curValues[j] + "</td></tr>");
                        }
                        else {
                            $(row).before("<tr><td colspan=" + columnCount + "><div class='blank' style='width:" + 9 * j + "px'></div><div class='minus' groupby='" + category + "' title='" + curValues[j] + "'></div><div class='blank'></div>" + curValues[j] + "</td></tr>");
                        }
                        preValues[j] = curValues[j];
                        for (l = j + 1; l < groups.length; l++)
                            preValues[l] = "";
                    }
                    $(row).find("TD").eq(group).hide();
                    precategory = category;
                    console.log(precategory, 'precategory');
                });
                $(row).addClass(precategory);
                $(row).find("TD").eq(0).before("<td colspan=" + groups.length + " class='left_space'></td>");
            });
            // 通过点击修改表格的显示隐藏
            var minus = "#" + id + " tbody tr .minus";
            console.log($(minus));
            $(minus).each(function (i, img) {
                if (settings.collapse == 1) {
                    $(this).addClass("plus");
                    $("#" + id + " ." + this.groupby).hide();
                }
                $(img).click(function () {
                    if ($(this).hasClass("plus")) {
                        $(this).removeClass("plus");
                        var groupby = $(this).attr("groupby");
                        var subcategoryCount = $("#" + id + " ." + groupby + " TD[colspan=" + columnCount + "]").length;

                        if (subcategoryCount > 0) {
                            $("#" + id + " ." + groupby + " TD[colspan=" + columnCount + "]>div").filter(".minus").addClass("plus");
                            $("#" + id + " ." + groupby + " TD[colspan=" + columnCount + "]").parent().show();
                        }
                        else
                            $("#" + id + " ." + groupby).show();
                    }
                    else {
                        group_hide(this);
                    }
                });
            });
            function group_hide(img_button) {
                $(img_button).addClass("plus");
                var groupby = $(img_button).attr("groupby");
                $("#" + id + " ." + groupby).hide();
                var subcategoryCount = $("#" + id + " ." + groupby + " TD[colspan=" + columnCount + "]").length;
                if (subcategoryCount > 0) {
                    $("#" + id + " ." + groupby + " TD[colspan=" + columnCount + "]>div").filter(".minus").each(function () {
                        group_hide(this);
                    });
                }
            }
        }
    });
})(jQuery);